package com.suirich.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suirich.bean.Enquiry;
import com.suirich.common.Constants;
import com.suirich.common.Page;
import com.suirich.service.EnquiryService;

@Controller
@RequestMapping("/enquiry")
public class EnquiryController {
	@Autowired
	private EnquiryService enquiryService;

    @RequestMapping(value="/create",method=RequestMethod.POST)
    @ResponseBody
	public Map<String,String> create(Enquiry enquiry){

        Map<String,String> result = new HashMap<String,String>();
        
		try {
		    enquiry.setCreateDate(new Date());
		    
			this.enquiryService.create(enquiry);
			
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
		} catch (Exception e) {
			
            result.put("result", Constants.FAILURE_RESULT_FLAG);
		}

        return result;
	}
    
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
	public Map<String,String> delete(Enquiry enquiry){

        Map<String,String> result = new HashMap<String,String>();
        
		try {
			this.enquiryService.delete(enquiry);
			
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
		} catch (Exception e) {
			
            result.put("result", Constants.FAILURE_RESULT_FLAG);
		}

        return result;
	}
    
    @RequestMapping(value="/{enquiryId}",method=RequestMethod.GET)
    @ResponseBody
	public Map<String,Object> show(@PathVariable("enquiryId")Long enquiryId){

        Map<String,Object> result = new HashMap<String,Object>();
        
		try {
		    Enquiry enquiry = this.enquiryService.read(enquiryId);
		    
            result.put("enquiry", enquiry);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
		} catch (Exception e) {
            result.put("result", Constants.FAILURE_RESULT_FLAG);
		}
		
        return result;
	}
    
    @RequestMapping(value="/list",method=RequestMethod.GET)
    @ResponseBody
	public Map<String,Object> list(Page page){
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			List<Enquiry> enquiryList = this.enquiryService.getEnquiryList(page);
			Long enquiryListCount = this.enquiryService.getEnquiryListCount();

			result.put("enquiryList", enquiryList);
			result.put("enquiryListCount", enquiryListCount);
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
		} catch (Exception e) {
            result.put("result", Constants.FAILURE_RESULT_FLAG);
		}
		
        return result;
	}
}
